#!C:\Python26

from pluginManage import Plugin
import re
import os
import sys
import urllib
import urlparse
import webFunctions

def pprint(*args):
	print args
def pperr(*args):
	print "Error - ", args

class ThreadWalker(Plugin):

	#Configuration Variables - Loaded when plugin is selected
	capabilities		=	"wScraperDisabled"											#Used for detecting ifany random .py file is a plugin file
	version			=	1.2												#Used for detecting available configuration options
	pluginName		=	"WWOECvBulletinScraper"										#Plugin Name variable
	siteName		=	"WWOEC BB Thread Scraper"									#Value for populating "Art Site" dropbox
	nameSource		=	["Single Thread", "Thread List File"]								#namesource value passed to plugin
	nameSourceEN		=	1												#Determines if Name Source input box is enabled (Boolean, 0-1)
	artistListEN		=	1												#Determines if Artist List input box is enabled (Boolean, 0-1)
	overwriteModes		=	["Check Files", "Overwrite Files"]								#Determines if overwrite mode dropbox is enabled for plugin
	retrievalHandles	=	1												#How many http retrieval handles app wants

	DLFolder		=	"WWOEC Thread Scraper"
	logFileName		=	"LogFile.txt"

	aNameLabeltxt		=	"Main Thread Page: "
	aListLabeltxt		=	"Number of Pages: "

	cookieCapable		=	1
	instructionText		=	"Nothing Yet"

	def checkCookie(self, cookiejar):
		userID = re.search("<Cookie bbpassword=[\w%]*? for www\.wwoecforum\.com/>", "%s" % cookiejar)
		sessionID = re.search("<Cookie bbuserid=[0-9]*? for www\.wwoecforum\.com/>", "%s" % cookiejar)
		if userID and sessionID:
			return "Have WWOEC Cookies:\n	%s\n	%s" % (userID.group(0), sessionID.group(0))

		return "Do not have WWOEC login Cookies"

	def getCookie(self, userName, password, opener):


		params = urllib.urlencode({"vb_login_username" : userName, "vb_login_password" : password, "cookieuser" : "1", "s" : "",\
					"ref" : "http://www.wwoecforum.com/index.php\r\n", "do" : "login", "securitytoken" : "guest",\
					"vb_login_md5password" : "", "vb_login_md5password_utf" : ""})


		page = opener.open('http://www.wwoecforum.com/login.php?do=login', params)
		pagetext = page.read()
		page.close()
		#print pagetext
		if re.search("You have entered an invalid username or password", pagetext):
			return "Login Failed"
		else:
			return "Logged In"



	def getThread(self, config, getHandles, threadURL):											 #Main Function Call - Uses other functions
		#pprint(config)
		#pprint("Artist Name :"+threadURL)
		#pprint("Artist List :"+config.aList)
		#pprint("Name Source :"+config.nSrcMode)
		#pprint("Overwrite Mode :"+config.ovwMode)
		pprint(getHandles)


		regexThreadNumber = re.compile(".*?=(\d*?)$")
		threadNum = (re.search(regexThreadNumber, threadURL)).group(1)
		threadNumberStr = ("Thread Number %s" % threadNum)
		pprint("%s" % threadNumberStr)

		self.DLFolderFull = os.path.join(self.DLFolder, threadNumberStr)
		print self.DLFolderFull
		if not os.access(self.DLFolderFull, os.W_OK):
				try:
					os.mkdir(self.DLFolderFull)
					pprint("Created folder "+self.DLFolderFull)
					pprint("Beginning Download")
				except:
					pprint("Cannot Make working directory ("+self.DLFolderFull+"/. Do you have write Permissions?", sys.exc_info()[0])
					return 0
		#print threadURL+"EOL"
		#print "Hello"
		#pprint( os.path.join(self.DLFolderFull, "Thread Number - %s.html" % ((re.search('\d*?\Z', threadURL)).group(0))))
		print "Made Folders"


		logFile = os.path.join(self.DLFolderFull, self.logFileName)
		print logFile
		logptr = open(logFile, "a")

		counter = 1

		overallPage = ""

		tempPage1 = getHandles.getpage("%s&page=%s" % (threadURL, counter))
		if tempPage1 == "Failed":
			pprint("Error : Could not get page 1. Failed")
			pprint("%s&page=%s" % (threadURL, counter))
			return 0


		tempPage1 = handle.read()

		overallPage = overallPage + tempPage1


		pageTitleRE = re.compile('<title>.*?</title>')
		previousTitle = (re.search(pageTitleRE, tempPage1)).group(0)

		while 1:							#walk over forum thread, and concatenate all the pages into one giant string for scraping later
			counter = counter + 1

			handle = getHandles.getpage("%s&page=%s" % (threadURL, counter), returnHandle = True)
			if handle == "Failed":
				pprint("Error : Could not get page not %s page. Failed" % counter)
				return 0

			tempPage2 = handle.read()

			currentTitle = (re.search(pageTitleRE, tempPage2)).group(0)
			#pprint("Current Title = \"%s\"" % currentTitle)
			if currentTitle == previousTitle:
				break
			else:
				overallPage = overallPage + tempPage2
				previousTitle = currentTitle

		pprint("Done downloading Pages")

		#print overallPage
		try:
			pageFileName = os.path.join(self.DLFolderFull, "Thread string.html")
		except:
			pperr("Error making pageFileName")
			return 0

		pageFile = open(pageFileName, "w")
		pageFile.write(overallPage)
		pageFile.close()

		links = re.findall('a href=\"(attachment\.php.*?)\"', overallPage)
		#print overallPage

		rootURL = urlparse.urlparse(threadURL)
		fqLinks = []
		for link in links:

			plink = urlparse.urlunsplit((rootURL[0], rootURL[1], link, "", ""))
			fqLinks.append(plink)
			pprint("Link - %s" % plink)

		print fqLinks
		fNameRE = re.compile(".*?\"(.*?)\"")


		self.progBars.setSubBarLen(len(fqLinks))
		self.progBars.setSubBar(0)

		for link in fqLinks:

			image = getHandles.getpage(link, returnHandle = True)
			fileName = fNameRE.search(image.info()["content-disposition"]).group(1)
			print fileName
			if (not os.access(os.path.join(self.DLFolderFull, fileName), os.W_OK)) or config.ovwMode == "Overwrite Files" :

				filePath = os.path.join(self.DLFolderFull, fileName)

				fileHandle = open(filePath, "wb")


				tempFile = image.read()
				fileHandle.write(tempFile)
				fileHandle.close()
			else:
				pprint("Already Have $s" % filePath)
			self.progBars.incSubBar()

		return 1
	def main(self, config, getHandles):											 #Main Function Call - Uses other functions

		progBars = webFunctions.ProgressBars()
		self.DLFolder = os.path.join(config.settings.downloadDir, self.DLFolder)

		if not os.access(self.DLFolder, os.W_OK):
				try:
					os.mkdir(self.DLFolder)
					pprint("Created folder "+self.DLFolder)
					pprint("Beginning Download")
				except:
					pprint("Cannot Make working directory ("+self.DLFolder+"/. Do you have write Permissions?", sys.exc_info()[0])
					return 0
		self.progBars = webFunctions.ProgressBars()

		self.progBars.setMainBarLen(len(config.aList))
		self.progBars.setMainBar(0)

		for name in config.aList:
			pprint(name)
			self.getThread(config, getHandles, name)
			self.progBars.incMainBar()
